'''
1、网址：https://pvp.qq.com/web201605/herolist.shtml
'''

# 导入提供的工具类
import urllib.request
import os
import json

# 调用工具类中的获取英雄信息的方法获取英雄数据
response = urllib.request.urlopen("http://pvp.qq.com/web201605/js/herolist.json")
hero_json = json.loads(response.read())

# 传入英雄数据，封装成指定格式并保存到文件中
with open("./save_file.txt", 'w', encoding='utf-8') as file:
    for hero in hero_json:
        message = '英雄：%s,皮肤：%s\n' % (hero['cname'], hero['skin_name'].split('|'))
        file.write(message)




# 传入英雄数据，下载英雄皮肤并保存到指定的heroskinimg目录下

for hero in hero_json:
        skin_name = hero['skin_name'].split('|')
        for i in range(len(skin_name)):
            # 构建图片名称
            save_file_name = "heroskinimg\\" + str(hero['ename']) + '-' + hero['cname'] + '-' + skin_name[i] + '.jpg'
            # 根据皮肤图片规律拼接完整皮肤路径
            skin_url = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' + \
                       str(hero['ename']) + '/' + str(hero['ename']) + \
                       '-bigskin-' + str(i + 1) + '.jpg'
            if not os.path.exists(save_file_name):
                # 抓取皮肤图片
                urllib.request.urlretrieve(skin_url, save_file_name)


